#include "../curl/CurlClient.h"
#include "../http/HttpData.h"
#include "../common/global.h"
#include "../common/CCReactor.h"
#include <iostream>
#include <string>
#include <thread>
#include <chrono>

int main() {
    std::cout << "HTTP Client Example" << std::endl;
    
    // Initialize global components
    g_reactor->Init();
    g_reactor->Start();
    
    // Create HTTP client
    CurlClient client;
    
    // Initialize client
    if (!client.Initialize()) {
        std::cerr << "Failed to initialize client" << std::endl;
        return 1;
    }
    
    // Test synchronous request
    std::cout << "\n=== Synchronous Request ===" << std::endl;
    
    stHttpRequest sync_req;
    sync_req.method = "GET";
    sync_req.url = "http://httpbin.org/get";
    sync_req.heads["User-Agent"] = "HttpLib/1.0";
    
    stHttpResponse sync_resp;
    int result = client.Request(std::move(sync_req), sync_resp);
    
    if (result == 0) {
        std::cout << "Response status: " << sync_resp.state << std::endl;
        std::cout << "Response headers:" << std::endl;
        for (const auto& header : sync_resp.heads) {
            std::cout << "  " << header.first << ": " << header.second << std::endl;
        }
        std::cout << "Response body: " << sync_resp.body.substr(0, 200) << "..." << std::endl;
    } else {
        std::cerr << "Request failed with error: " << result << std::endl;
    }
    
    // Test asynchronous request
    std::cout << "\n=== Asynchronous Request ===" << std::endl;
    
    client.SetResponseCb([](std::uint64_t id, int err, stHttpResponse&& resp) {
        std::cout << "Async response received for request " << id << std::endl;
        if (err == 0) {
            std::cout << "Response status: " << resp.state << std::endl;
            std::cout << "Response body: " << resp.body.substr(0, 200) << "..." << std::endl;
        } else {
            std::cerr << "Async request failed with error: " << err << std::endl;
        }
    });
    
    stHttpRequest async_req;
    async_req.method = "POST";
    async_req.url = "http://httpbin.org/post";
    async_req.heads["Content-Type"] = "application/json";
    async_req.heads["User-Agent"] = "HttpLib/1.0";
    async_req.body = "{\"message\": \"Hello from async client!\"}";
    
    static uint64_t next_id = 1;
    client.RequestAsync(next_id++, std::move(async_req));
    
    // Wait for async response
    std::cout << "Waiting for async response..." << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(3));
    
    // Test POST request
    std::cout << "\n=== POST Request ===" << std::endl;
    
    stHttpRequest post_req;
    post_req.method = "POST";
    post_req.url = "http://httpbin.org/post";
    post_req.heads["Content-Type"] = "application/json";
    post_req.heads["User-Agent"] = "HttpLib/1.0";
    post_req.body = "{\"name\": \"test\", \"value\": 123}";
    
    stHttpResponse post_resp;
    result = client.Request(std::move(post_req), post_resp);
    
    if (result == 0) {
        std::cout << "POST response status: " << post_resp.state << std::endl;
        std::cout << "POST response body: " << post_resp.body.substr(0, 300) << "..." << std::endl;
    } else {
        std::cerr << "POST request failed with error: " << result << std::endl;
    }
    
    // Cleanup
    g_reactor->Stop();
    
    return 0;
}